<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../teacher/js/axios.min.js"></script>
</head>
<body>
这是一个登录页面
<br>
用户名:<input type="text" id="username" value="张三">
<br>
密码: <input type="text" id="password" value="123456">
<br>
<button onclick="dl()">登录</button>
&emsp;
<a href="/api/qqLogin/qq">QQ登录</a>


<br><br><br>
<button onclick="hqxx()">登录之后获取用户信息</button>
<div id="userinfo">

</div>
<br><br><br>
<a href="test.html">测试页面</a>
</body>
<script>

    /**
     * 登录
     */
    function dl() {
        let params = new URLSearchParams();
        params.append("grant_type", "password");
        params.append("username", username.value);
        params.append("password", password.value);
        axios({
            method: 'post',
            url: '/oauth/token',
            data: params,
            headers: {
                "Authorization": "Basic aWVwOmllcHNlY3JldA=="
            }
        }).then(res => {
            res = res.data;
            //token
            sessionStorage.setItem("access_token", res.access_token);
            //刷新token
            sessionStorage.setItem("refresh_token", res.refresh_token);
            //过期时间
            sessionStorage.setItem("expires_in", res.expires_in);
            //权限
            let authorities=res.authorities;
            console.log(res);
            sessionStorage.setItem("authorities",authorities);
            let url=authoritiesToUrl(authorities);
            axios.defaults.headers.common['Authorization'] = "bearer " + res.access_token;
        })
    }
    function authoritiesToUrl(authorities) {
        let role="";
        for (let i = 0; i < authorities.length; i++) {
            console.log(authorities[i])
            if(authorities[i].indexOf("ROLE")>=0){
                role=authorities[i].substring(5);
                break;
            }
        }
        console.log(role)
    }
    function hqxx() {
        axios({
            method: 'get',
            url: '/api/oauth/me'
        }).then(res => {
            userinfo.innerHTML = JSON.stringify(res.data)
        })
    }
</script>
</html>